<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="utf-8">
  <title>菜单管理</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="static/layui/css/layui.css" media="all">
  <link rel="stylesheet" href="static/layuiadmin/style/admin.css" media="all">

</head>
<body>
<!--templates/table/cdgl-->
<div class="layui-fluid">
  <div class="layui-row layui-col-space15">
    <div class="layui-col-md12">
      <div class="layui-card">
        <div class="layui-card-header card-title">菜单管理</div>
        <div class="layui-card-body">
          <div class="layui-btn-group test-table-operate-btn" style="margin-bottom: 10px;">
            <a class="layui-btn btn-add btn-default" id="btn-add">添加</a>
          </div>
          <table class="layui-hide" id="test-table-cellEdit" lay-filter="test-table-cellEdit"></table>
          <script type="text/html" id="test-table-operate-barDemo">
            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
          </script>
        </div>
      </div>
    </div>
  </div>
</div>

<script src="static/layui/layui.js"></script>
<script th:inline="none">
  layui.config({//设定扩展的 layui 模块的所在目录
    base: 'static/layuiadmin/' //静态资源所在路径
  }).extend({//给index路径建立别名
    index: 'lib/index' //主入口模块
  }).use(['index', 'table'], function() {
    var table = layui.table;
    var loading = layer.load();
    table.render({
      elem: '#test-table-cellEdit'
      , url: "/selectcdgl"
      , cols: [[
        {field: 'id', title: 'ID', width: 80, sort: true}
        , {field: 'menuname', title: '菜单名', width: 120, edit: 'text', sort: true}
        , {field: 'menulink', title: '菜单链接', width: 80, edit: 'text'}
        , {field: 'fdm', title: '父菜单', minWidth: 150, sort: true}
        , {width: 178, align: 'center', fixed: 'right', toolbar: '#test-table-operate-barDemo'}
      ]]
      , done: function () {
        layer.close(loading);
      }
    })

    //监听单元格编辑
    table.on('edit(test-table-cellEdit)', function (obj) {
      var value = obj.value //得到修改后的值
              , data = obj.data //得到所在行所有键值
              , field = obj.field; //得到字段
      $.ajax({
        url: "/upcdgl",
        data: {id: data.id, field: field, updateValue: value},
        type: "post",
        dataType: "json",
        success: function (result) {
          layer.msg('修改成功', {icon: 1});
        },
        error: function (e) {
          layer.msg('修改失败', {icon: 2});
        }
      })
    });

    //监听工具条
    table.on('tool(test-table-cellEdit)', function (obj) {
      var data = obj.data;
      if (obj.event === 'del') {
        layer.confirm('真的删除么', function (index) {
          $.ajax({
            url: "/delcdgl",
            data: {id: data.id},
            type: "post",
            dataType: "json",
            success: function (result) {
              layer.msg('已删除', {icon: 1});
              obj.del();
            },
            error: function (e) {
              layer.msg('删除失败', {icon: 2});
            }
          })
          layer.close(index);
        });
      }
    });
    var $ = layui.$;
    $('#btn-add').on('click', function () {
      layer.open({
        type: 2,
        title: '添加菜单',
        maxmin: true,
        area: ['500px', '450px'],
        shadeClose: false, //点击遮罩关闭
        content: 'Jumpto?url=insert/cdgl',
      });
    });
  })
</script>

</body>
</html>